/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  							 script_display_text.cpp

	$Header: /heroes4/script_display_text.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "script_display_text.h"

#include "adventure_map.h"
#include "adventure_map_window.h"
#include "basic_dialog.h"
#include "counted_ptr.h"
#include "external_string.h"
#include "player.h"
#include "streambuf_operators.h"


// This dialog point was made global so I could maintain the 
// const modifier on the execute() member functions.  If the
// const was to be removed it could be made a member of the 
// class itself.
namespace {
	static t_counted_ptr<t_basic_dialog> g_display_text_dlg_ptr;
};
	
// --------------------------------------------------------------------------
// t_script_display_text members
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_script_display_text::read_from_map(
	std::streambuf &	stream,
	int					format_version )
{
	return		read_string16( stream, m_text )
			&&	(	format_version < 1
				||	t_script_sequence::read_from_map( stream, format_version ) );
}

bool t_script_display_text::read( std::streambuf& stream, int version)
{
	if (t_script_sequence::read( stream, version ) == false)
		return false;

	return read_string16( stream, m_text );
}

bool t_script_display_text::write( std::streambuf& stream ) const
{
	if (!t_script_sequence::write( stream )) 
		return false;
	
	return write_string16( stream, m_text );
}

void t_script_display_text::execute_dialog(t_basic_dialog* dialog) const
{
	if (m_text.empty()) return;

	dialog->set_text( m_text, k_justify_left );
	dialog->add_ok_button();
	dialog->open();
	dialog->run_modal();
}

void t_script_display_text::execute(t_script_context_hero const& context) const
{
	g_display_text_dlg_ptr = new t_basic_dialog;
	t_script_sequence::execute(context);
	t_player const& player = context.map->get_current_player();
	if ((!player.is_computer()))
		execute_dialog(g_display_text_dlg_ptr);
	g_display_text_dlg_ptr = NULL;
}

void t_script_display_text::execute(t_script_context_town const& context) const
{
	g_display_text_dlg_ptr = new t_basic_dialog;
	t_script_sequence::execute(context);
	t_player const& player = context.map->get_current_player();
	if ((!player.is_computer()))
		execute_dialog(g_display_text_dlg_ptr);
	g_display_text_dlg_ptr = NULL;
}

void t_script_display_text::execute(t_script_context_object const& context) const
{
	g_display_text_dlg_ptr = new t_basic_dialog;
	t_script_sequence::execute(context);
	t_player const& player = context.map->get_current_player();
	if ((!player.is_computer()))
		execute_dialog(g_display_text_dlg_ptr);
	g_display_text_dlg_ptr = NULL;
}

void t_script_display_text::execute(t_script_context_army const& context) const
{
	assert(context.map);
	g_display_text_dlg_ptr = new t_basic_dialog;
	t_script_sequence::execute(context);
	t_player const& player = context.map->get_current_player();
	if (context.map->is_game_over())
		return;
	context.map->get_map_window()->halt_movement();
	if ((!player.is_computer()))
		execute_dialog(g_display_text_dlg_ptr);
	g_display_text_dlg_ptr = NULL;

}

void t_script_display_text::execute(t_script_context_global const& context) const
{
	g_display_text_dlg_ptr = new t_basic_dialog;
	t_script_sequence::execute(context);
	t_player const& player = context.map->get_current_player();
	if ((!player.is_computer()))
		execute_dialog(g_display_text_dlg_ptr);
	g_display_text_dlg_ptr = NULL;
}

void t_script_display_text::executed_subaction(const t_subaction& subaction) const
{
	assert(g_display_text_dlg_ptr.get() != NULL);
	subaction.add_icons(g_display_text_dlg_ptr);
}

